from flask_restplus import Resource, Namespace, fields, marshal_with
from common.utils.decorators import login_required
from flask import g, request, jsonify, Response
from app.models import db
from app import redis_clent, app
from common.utils.image_tools import delete_image
from app.routes.park_collection.schemas import ParkingCollectionSchemas
from common.utils.backends import AliyunOssStorage
from app.models.collection import (
    ParkingCollection,
    ParkingCollectionImage,
)
from .marshmallow import (
    ParkingCollectionQuerySerializer,
    ParkingCollectionDataSerizlizer,
    ParkingCollectionBodySerizlizer,
    ParyingCollectionPostSerizlizer,
    parkingCollectionIdSerizlizer,
    ParkingCollectionPutSerizlizer,
    ParkingCollectionImagePostQuerySerizlizer,
    ParkingCollectionImageIdSerizlizer,
)

parking_collection_ns = Namespace("parking-collection")
parking_collection_schema = ParkingCollectionSchemas(namespace=parking_collection_ns)

ali_oss = AliyunOssStorage()


class ParkingCollectionImageView(Resource):
    @parking_collection_ns.expect(parking_collection_schema.ParyingCollectionImageCreateSchemas())
    @parking_collection_ns.marshal_with(parking_collection_schema.ParyingCollectionImageIdSchemas())
    def post(self):
        image_query = ParkingCollectionImagePostQuerySerizlizer()
        image_body = ParkingCollectionImageIdSerizlizer()

        image_query.context["request"] = request

        image = image_query.load(data=request.form)
        response = image_body.dump(image)
        return response


class ParkingCollectionImageDeleteView(Resource):
    def delete(self, id):

        try:
            image = ParkingCollectionImage.query.get(id)
        except Exception as e:
            raise e
        db.session.delete(image)

        delete_image(image.file)
        db.session.commit()

        return Response(status=204 if image else 500)


class ParyingCollectionChangeView(Resource):
    method_decorators = [login_required]

    def delete(self, id):
        try:
            parking = ParkingCollection.query.get(id)
        except Exception as e:
            raise e
        parking.deleted = True
        db.session.commit()

        return Response(status=204 if parking else 500)

    @parking_collection_ns.expect(parking_collection_schema.ParyingCollectionPostSchemas())
    def put(self, id):

        request.json["id"] = id
        collection = ParkingCollectionPutSerizlizer().load(request.json)

        return Response(status=202 if collection == 1 else 500)

    @parking_collection_ns.marshal_with(parking_collection_schema.ParyingCollectionInfoSchemas())
    def get(self, id):
        # 查询记录详情
        try:
            collection = ParkingCollection.query.get(id)
        except Exception as e:
            raise e

        response = ParkingCollectionBodySerizlizer().dump(collection)

        return response


class ParyingCollectionListView(Resource):

    method_decorators = [login_required]  # 检查用户登录的装饰器

    @parking_collection_ns.marshal_with(
        parking_collection_schema.ParyingCollectionBodySchemas()
    )  # 指定序列化返回
    @parking_collection_ns.doc(
        params=parking_collection_schema.ParyingCollectionListQuerySchemas()
    )  # 指定输入字段
    def get(self):

        data, total = ParkingCollectionQuerySerializer().load(data=request.args)
        response = ParkingCollectionDataSerizlizer().dump({"data": data, "total": total})

        return response

    @parking_collection_ns.expect(parking_collection_schema.ParyingCollectionPostSchemas())
    @parking_collection_ns.marshal_with(parking_collection_schema.ParyingCollectionIdSchemas())
    def post(self):
        """创建记录"""
        # data = hello_ns.payload

        collection = ParyingCollectionPostSerizlizer().load(data=request.json)
        response = parkingCollectionIdSerizlizer().dump(collection)
        return response
